/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 10:27:23
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-02-18 16:06:24
 * @FilePath: \cxl-driving-training\src\views\drivingSchool\siteConfig\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import {
    siteConfigAdd,
    siteConfigDetailed,
    siteConfigDisable,
    siteConfigEdit,
    siteConfigList,
    siteConfigListInterface,
    siteConfigOperationInterface
} from '@/apis/siteConfig'
import { DICT_DATA, optionsInterface } from '@/utils/dict-data'

export default class CompanyAdminData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '新增配置',
        width: '800px',
        id: '',
        type: 'create'
    })
    dialogForm = reactive({
        schoolId: '',
        detailed: ''
    })
    //科目二
    twoForm = reactive({
        name: '',
        type: '',
        typeName: '',
        addr: '',
        id: '',
        siteType: '',
        siteTypeName: ''
    })
    //科目三
    threeForm = reactive({
        name1: '',
        type1: '',
        addr1: '',
        id1: '',
        name2: '',
        type2: '',
        addr2: '',
        id2: '',
        visible: [true, false]
    })
    rules = {
        schoolId: [{ required: true, message: '请选择驾校', trigger: 'change' }],
        name1: [{ required: true, message: '请选择场地名称', trigger: 'change' }],
        type1: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        addr1: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        name2: [{ required: true, message: '请选择场地名称', trigger: 'change' }],
        type2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        addr2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
    }
    selectSubject = ref('科目二')
    //驾校
    drivingSchoolOptions = reactive<optionsInterface[]>([])

    searchTrainSiteName = reactive({
        width: '100%',
        name: '',
        disabled: false
    })
    searchTrainSiteName2 = reactive({
        width: '100%',
        name: '',
        disabled: false
    })
    searchData = {
        schoolName: '',
        siteThreeName: '',
        siteTwoName: '',
        status: ''
    }
    constructor() {
        super(tableConfig)

        this.drivingSchoolOptions.length = 0

        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                this.drivingSchoolOptions.push({ label: item.shortName, value: item.id })
            })
        })

        this.getData()
    }
    getData = (arg?: any) => {
        const params: siteConfigListInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.searchData.schoolName) {
            params.schoolName = this.searchData.schoolName
        }
        if (this.searchData.siteThreeName) {
            params.siteThreeName = this.searchData.siteThreeName
        }
        if (this.searchData.siteTwoName) {
            params.siteTwoName = this.searchData.siteTwoName
        }
        if (this.searchData.status !== '' && this.searchData.status !== '-1') {
            params.disable = Number(this.searchData.status)
        }
        siteConfigList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records
                console.log(this.uStore.drivingSchoolSimple)
                arr.forEach((element: any) => {
                    element.imageUrl = element.headPicUrl
                    element.status = Number(element.disable) === 0 ? true : false
                    // element.school = this.uStore.drivingSchoolSimple.get(element.schoolId)?.fullName
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            schoolName: '',
            siteThreeName: '',
            siteTwoName: '',
            status: ''
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }

    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type)
        this.dialog.type = type
        if (type === 'create') {
            this.dialog.title = '新增配置'
            this.dialog.visible = true
            this.dialogForm.schoolId = ''
        } else if (type === 'edit') {
            this.dialog.title = '修改配置'
            this.dialog.id = data.id
            siteConfigDetailed(data.id).then(res => {
                if (Number(res.data.code) === 200) {
                    this.dialog.visible = true
                    this.dialogForm.schoolId = res.data.result.schoolId + ''
                    this.searchTrainSiteName.name = res.data.result.siteTwoInfo.name
                    this.setTwoData(res.data.result.siteTwoInfo)
                    this.setThreeData1(res.data.result.siteThreeInfos)
                    this.setThreeData2(res.data.result.siteThreeInfos)
                }
            })
        } else if (type === 'switch') {
            siteConfigDisable(data.status ? 0 : 1, data.id).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('修改成功')
                    this.getData()
                }
            })
        }
    }
    //监听弹窗关闭
    onCloseDialog = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        formEl.resetFields()
        this.dialog.visible = false
    }
    //监听弹窗点击确定按钮
    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log('submit!', this.dialogForm.schoolId, this.twoForm, this.threeForm)
                const data: siteConfigOperationInterface = {
                    siteThreeIdList: []
                }
                if (!this.dialogForm.schoolId) {
                    ElMessage.error('请选择驾校')
                    return
                }

                data.schoolId = this.dialogForm.schoolId
                data.siteTwoId = this.twoForm.id

                if (this.threeForm.id1) {
                    data.siteThreeIdList?.push(this.threeForm.id1)
                }
                if (this.threeForm.id2) {
                    data.siteThreeIdList?.push(this.threeForm.id2)
                }

                console.log('data', data)
                if (this.dialog.type === 'create') {
                    siteConfigAdd(data).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            this.getData()
                            ElMessage.success('新增成功')
                        }
                    })
                } else {
                    data.id = this.dialog.id
                    siteConfigEdit(data).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            this.getData()
                            ElMessage.success('修改成功')
                        }
                    })
                }
            } else {
                console.log('error submit!', fields)
            }
        })
    }
    //科目三弹窗，点击+添加地址
    onClickAddSite = (index: number) => {
        if (index === 0) {
            this.threeForm.visible[1] = true
        } else if (index === 1) {
            this.threeForm.visible[0] = true
        }
    }
    onclickDeleteSite = (index: number) => {
        this.threeForm.visible[index] = false
    }
    onRadioGroupChange = (res: any) => {
        console.log(res)
        this.selectSubject.value = res
        this.searchTrainSiteName.name = ''
        this.searchTrainSiteName2.name = ''
        if (res === '科目二' && this.twoForm.name) {
            this.searchTrainSiteName.name = this.twoForm.name
        }
        if (res === '科目三' && this.threeForm.name1) {
            this.searchTrainSiteName.name = this.threeForm.name1
        }
        if (res === '科目三' && this.threeForm.name2) {
            this.searchTrainSiteName2.name = this.threeForm.name2
        }
    }
    setTwoData = (res: any) => {
        this.twoForm.name = res.name
        this.twoForm.addr = res.addressDetail
        this.twoForm.type = res.siteCategory
        this.twoForm.id = res.id
        this.twoForm.siteType = res.siteType
        const list: any[] = DICT_DATA.get('siteType')
        const result = list.find(item => {
            return Number(item.value) === res.siteType
        })
        this.twoForm.siteTypeName = result.label || ''
    }
    setThreeData1 = (res: any) => {
        this.threeForm.name1 = res.name
        this.threeForm.addr1 = res.addressDetail
        this.threeForm.type1 = res.siteCategory
        this.threeForm.id1 = res.id
    }
    setThreeData2 = (res: any) => {
        this.threeForm.name2 = res.name
        this.threeForm.addr2 = res.addressDetail
        this.threeForm.type2 = res.siteCategory
        this.threeForm.id2 = res.id
    }
    onSelectTrainSiteName = (res: any) => {
        console.log(res)
        if (this.selectSubject.value === '科目二') {
            this.searchTrainSiteName.name = res.name
            this.setTwoData(res)
        } else if (this.selectSubject.value === '科目三') {
            this.searchTrainSiteName.name = res.name
            this.setThreeData1(res)
        }
    }
    onSelectTrainSiteName2 = (res: any) => {
        console.log(res)
        this.searchTrainSiteName2.name = res.name
        this.setThreeData2(res)
    }
}
